//
//  NetworkTools.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/21.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import AFNetworking

// 反转的域名
let dataErrorDomain = "com.baidu.data.error"

//枚举任意类型
enum HTTPMethod: String {
    case GET = "GET"
    case POST = "POST"
}

// MARK: 网络请求的处理层
// 以后网络请求都使用这个类,来进行数据的获取
class NetworkTools: AFHTTPSessionManager {
    // 单例对象
    static let sharedTools: NetworkTools = {
       
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        
        let tools = NetworkTools(baseURL: url)
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    
    // 网络请求的核心方法封装
    func requestJSONDict(method: HTTPMethod, urlString: String, parameters: [String : String]?, finished: (dict: [String : AnyObject]?, error: NSError?) -> () ) {
        
        if method == HTTPMethod.POST {
            
            // 发送POST请求
            POST(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
//                    print(result)
                    // 请求成功的回调
                    
                    guard let dict = result as? [String : AnyObject] else {
                        // 不能转换为字典数据
                        // 执行失败的回调
                        let err = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        
                        print(err)
                        
                        finished(dict: nil, error: err)
                        
                        return
                    }
                    
                    // 执行成功的回调
                    finished(dict: dict, error: nil)
                    
                }) { (_, error) -> Void in
                    print(error)
                    // 执行失败的回调
                    finished(dict: nil, error: error)
            }
            
        } else {
            
            // 发送GET请求
            GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
//                    print(result)
                    // 请求成功的回调
                    
                    guard let dict = result as? [String : AnyObject] else {
                        // 不能转换为字典数据
                        // 执行失败的回调
                        let err = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        
                        print(err)
                        
                        finished(dict: nil, error: err)
                        
                        return
                    }
                      
                    // 执行成功的回调
                    finished(dict: dict, error: nil)
                    
                }) { (_, error) -> Void in
                    print(error)
                    // 执行失败的回调
                    finished(dict: nil, error: error)
            }
        }
    }
    
    // 上传图片的工具方法
    func uploadImage(urlString: String, parameters: [String : String]?, imageData: NSData, finished: (dict: [String : AnyObject]?, error: NSError?) -> ()) {
        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            formData.appendPartWithFileData(imageData, name: "pic", fileName: "xxoo", mimeType: "image/jpeg")
            }, progress: { (p) -> Void in
                print("progrees---\(p)")
            }, success: { (_, result) -> Void in
                print(result)
                // 上传成功
                if let dict = result as? [String : AnyObject] {
                    finished(dict: dict, error: nil)
                }
            }) { (_, error) -> Void in
                print(error)
                // 上传失败
                finished(dict: nil, error: error)
        }
    }
    
    
}



